<%--
  Created by IntelliJ IDEA.
  User: 进击的龟仙人
  Date: 2021/5/25
  Time: 10:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>进货信息</title>
    <%
        String path=request.getContextPath();
        String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <base href="<%=basePath%>">
    <link rel="stylesheet" href="layui-v2.6.5/layui/css/layui.css">
</head>
<body>
<table id="demo" lay-filter="test"></table>
<script src="layui-v2.6.5/layui/layui.js"></script>
<%--表头工具栏--%>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>

    layui.use(['table','util'], function(){
        var table = layui.table;
        util = layui.util;
        //第一个实例
        table.render({
            elem: '#demo'
            ,height: 520
            ,url: 'manager/purchaseServlet?action=list' //数据接口
            ,page: true //开启分页
            ,toolbar:"#toolbarDemo"
            ,cols: [[ //表头
                {type:'checkbox'},
                {field: 'number', type:"numbers"}
                ,{field: 'pid', title: '商品编号', width:300,edit:'true'}
                ,{field: 'prime', title: '进货单价', width:150, sort: true,edit:'true'}
                ,{field: 'etime', title: '进货时间', width:180,
                    }
                ,{field: 'enumber', title: '进货数量', width: 100,edit:'true'}
                ,{field: 'totalcost', title: '总价', width: 100, sort: true,edit:'true'}
                ,{field:"操作",toolbar:"#barDemo"}
            ]]
        })
        // 监听行事件
        table.on('tool(test)',function (obj) {
            var data=obj.data;
            if(obj.event==='del'){
                layer.confirm('真的删除吗？',function (index) {
                    obj.del();
                    $.ajax({
                        url:"manager/purchaseServlet?action=delete&eid="+data.eid,
                        type:"get",
                        contentType: 'application/json;charset=utf-8',
                        dataType: "text",
                        data:{
                            'eid':data.eid,
                            'pid':data.pid,
                            'prime':data.prime,
                            'etime':data.etime,
                            'enumber':data.enumber,
                            'totalcost':data.totalcost
                        },
                        success:function (res) {
                            layer.msg("删除成功")
                        },
                        error:function (res) {
                            layer.msg("删除失败")
                        }
                    });
                    layer.close(index);
                })
            }

        })
        //监听单元格编辑
        table.on('edit(test)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            layer.msg('[ID: '+ data.eid +'] ' + field + ' 字段更改为：'+ value);

            $.ajax({
                url:"manager/purchaseServlet?action=update&eid="+data.eid+"&pid="+data.pid+"&prime="+data.prime+"&etime="+data.etime+"&enumber="+data.enumber+"&totalcost="+data.totalcost,
                type:"post",
                contentType:'application/json;charset=utf-8',
                dataType:"text",
                data:{
                    'eid':data.eid,
                    'pid':data.pid,
                    'prime':data.prime,
                    'etime':data.etime,
                    'enumber':data.enumber,
                    'totalcost':data.totalcost
                },
                success:function (res) {
                    layer.msg('编辑成功');
                },
                error:function (res) {
                    layer.msg('编辑失败');
                }
            })
        })
    });
</script>
</body>
</html>
